package com.example.anjia03_3z.service.impl;


import com.example.anjia03_3z.exception.ServiceException;
import com.example.anjia03_3z.mapper.UserMapper;
import com.example.anjia03_3z.pojo.dto.UserAddNewDTO;
import com.example.anjia03_3z.pojo.dto.UserUpdateDTO;
import com.example.anjia03_3z.pojo.entity.User;
import com.example.anjia03_3z.pojo.vo.UserListItemVO;
import com.example.anjia03_3z.pojo.vo.UserStandardVO;
import com.example.anjia03_3z.service.IUserService;
import com.example.anjia03_3z.web.JsonPage;
import com.example.anjia03_3z.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper mapper;



    /**
     * 添加新的分类
     *
     * @param userAddNewDTO 获取的数据
     */
    public void addNew(UserAddNewDTO userAddNewDTO) {
        User user = new User();
        BeanUtils.copyProperties(userAddNewDTO,user);
        mapper.insert(user);
    }

    /**
     * @param
     * @return
     */
    public JsonPage<UserListItemVO> list(Integer page,Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<UserListItemVO> list = mapper.list();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public UserStandardVO getStandard(Long id) {
        UserStandardVO standardVO = mapper.getStandardById(id);


        if (standardVO == null) {
            String message = "数据已经不存在，请刷新";
            throw new ServiceException(message, ServiceCode.ERR_NOT_FOUND);
        }

        return standardVO;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }


    @Override
    public void updateById(Long id, UserUpdateDTO userUpdateDTO) {
        UserStandardVO standardById = mapper.getStandardById(id);
        if (standardById == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "数据库无该数据");
        }
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        int rows = mapper.update(user);
    }

}
